﻿using UnityEngine;

public abstract class UIPanel : UIViewBase, IUIPanel
{
    /// <summary>
    /// 生成并显示UIPanel
    /// </summary>
    /// <typeparam name="T">UIPanel派生类</typeparam>
    /// <param name="prefab">UIPanel预制体</param>
    /// <param name="layer">UI层级，默认Main</param>
    /// <returns>实例化的UIPanel</returns>
    public static T Show<T>(T prefab, UILayer layer = UILayer.Main) where T : UIPanel
    {
        if (prefab == null)
        {
            Debug.LogError("UIPanel prefab is null.");
            return null;
        }

        // 实例化
        T panel = Object.Instantiate(prefab);

        // 设置父节点到对应UI层
        var parent = UIRoot.Instance.GetLayer(layer);
        if (parent == null)
        {
            Debug.LogError($"UIRoot 未找到层级: {layer}");
            Object.Destroy(panel.gameObject);
            return null;
        }
        panel.transform.SetParent(parent, false);

        // 可扩展：注册到UIManager等
        var uiManager = GlobalRefs.AppManager.GetManager<UIManager>();
        if (uiManager != null)
        {
            uiManager.RegisterView(panel, layer);
        }

        return panel;
    }

    public void Show(UILayer layer)
    {
        var parent = UIRoot.Instance.GetLayer(layer);
        if (parent == null)
        {
            Debug.LogError($"UIRoot 未找到层级: {layer}");
            return;
        }
        transform.SetParent(parent, false);
        OnShow();
    }
}